﻿using System;
using Company.BLL;
using Company.BLL.KDT.GC;
using Company.BLL.Utils;
using Company.BLL.DuLieuChuan;
using Janus.Windows.GridEX;
using System.Windows.Forms;

namespace Company.Interface.KDT.GC
{
    public partial class DinhMucSelectNguyenPhuLieuForm : BaseForm
    {
        public HopDong hongdong = new HopDong();        
        public NguyenPhuLieuCollection nplCollection = new NguyenPhuLieuCollection();
        public NguyenPhuLieu npl = new NguyenPhuLieu();
        public bool isAdd = true;//luu tru thong tin cua them moi hay sua chua
        public bool isUpdate = false;//luu truu thong tin la da cap nhat du lieu vao csdl chua
        private int STTHang = 0;
        public bool isBrower = false;
        public DinhMucSelectNguyenPhuLieuForm()
        {
            InitializeComponent();
        }
        private void LoadDanhSachChuaNPL()
        {
            PhuKienDangKyCollection pkdkCollection = PhuKienDangKy.SelectCollectionBy_HopDong_IDAndMaPhuKienDaDuyet(hongdong.ID, "N01");
            PhuKienDangKyCollection pkdkCollectionNPLVietNam = PhuKienDangKy.SelectCollectionBy_HopDong_IDAndMaPhuKienDaDuyet(hongdong.ID, "N11");
            foreach (PhuKienDangKy pkdk in pkdkCollectionNPLVietNam)
            {
                pkdkCollection.Add(pkdk);
            }
            PhuKienDangKy pkHopDong = new PhuKienDangKy();
            pkHopDong.SoTiepNhan = 0;
            pkHopDong.MaPhuKien = "Hợp đồng";
            pkdkCollection.Add(pkHopDong);
            cbHopDong.DataSource = pkdkCollection;
            cbHopDong.DisplayMember = "MaPhuKien";
            cbHopDong.ValueMember = "ID";
            cbHopDong.SelectedIndex = pkdkCollection.Count - 1;
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void NguyenPhuLieuGCEditForm_Load(object sender, EventArgs e)
        {          
            dgList.AllowDelete = Janus.Windows.GridEX.InheritableBoolean.False;
            nplCollection = NguyenPhuLieu.SelectCollectionBy_HopDong_ID(hongdong.ID);         
            dgList.DataSource = nplCollection;
            LoadDanhSachChuaNPL();
        }

     

    
        private void dgList_DeletingRecord(object sender, Janus.Windows.GridEX.RowActionCancelEventArgs e)
        {
            if (hongdong.TrangThaiXuLy == TrangThaiXuLy.DA_DUYET)
            {                             
                ShowMessage("Hợp đồng đã duyệt chính thức. Không được chỉnh sửa",false);
                e.Cancel = true;
                return;        
            }
            if (e.Row.RowType == RowType.Record)
            {
                if (ShowMessage("Bạn có muốn xóa nguyên phụ liệu này không?", true) == "Yes")
                {                    
                    NguyenPhuLieu npl1 = new NguyenPhuLieu();
                    npl1.ID = (long)Convert.ToInt64(e.Row.Cells[1].Text);
                    if (npl1.ID > 0)
                    {
                        npl1.Delete();
                        isUpdate = true;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

   
        private void NguyenPhuLieuGCEditForm_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
           
        }

        private void dgList_RowDoubleClick(object sender, Janus.Windows.GridEX.RowActionEventArgs e)
        {
            npl.Ma = e.Row.Cells[3].Text.Trim();
            npl.Ten = e.Row.Cells[4].Text.Trim();
            npl.MaHS = e.Row.Cells[5].Text.Trim();
            npl.DVT_ID = e.Row.Cells[6].Text;
            npl.SoLuongDangKy = Convert.ToDecimal(e.Row.Cells[8].Text);
            npl.ID = (long)Convert.ToDecimal(e.Row.Cells[1].Text);
            npl.HopDong_ID = hongdong.ID;           
            this.Close();            
        }

        private void dgList_LoadingRow(object sender, RowLoadEventArgs e)
        {
            e.Row.Cells[7].Text = this.DonViTinh_GetName(e.Row.Cells[6].Value).Trim();
        }

        private void cbHopDong_ValueChanged(object sender, EventArgs e)
        {
            long id = Convert.ToInt64(cbHopDong.Value);
            if (id == 0)
            {
                nplCollection = new NguyenPhuLieuCollection();
                nplCollection = NguyenPhuLieu.SelectCollectionBy_HopDong_ID(hongdong.ID);
                dgList.DataSource = nplCollection;
            }
            else
            {
                PhuKienDangKy pkDuyet = (PhuKienDangKy)cbHopDong.DropDownList.GetRow().DataRow;
                nplCollection = new NguyenPhuLieuCollection();
                PhuKienCollection collection = PhuKien.SelectCollectionTrangThaiDuyetById_HopDong(hongdong.ID, pkDuyet.MaPhuKien, pkDuyet.SoPhuKien);
                foreach (PhuKien pk in collection)
                {
                    NguyenPhuLieu nplDS = new NguyenPhuLieu();
                    nplDS.Ma = pk.MaHang;
                    nplDS.MaHS = pk.MaHS;
                    nplDS.SoLuongDangKy = pk.SoLuong;
                    nplDS.STTHang = pk.STTHang;
                    nplDS.Ten = pk.TenHang;
                    nplDS.DVT_ID = pk.DVT_ID;
                    nplCollection.Add(nplDS);
                }
                dgList.DataSource = nplCollection;
            }           
        }
    }
}